import CreateStore from './createStore'
import { action, computed, makeObservable, observable } from 'mobx'
class TodoListStore{
  todos=[]
  constructor(todos){
    if(todos){
      this.todos=todos
    }
    makeObservable(this,{
      todos:observable,
      addApple:action,
      eat:action,
      filterAllGram:computed,
      filterAllNum:computed,
      filterEatGram:computed
    })
  } 
  addApple(){
   this.todos.push(new CreateStore())
   console.log(this.todos)
   window.localStorage.setItem('apple',JSON.stringify(this.todos))
  }
  eat(index){
    this.todos[index].eat=true
    
  }
  get filterAllGram(){
    return this.todos.map(todos=>todos.gram).reduce((acc,cur)=>{
        return acc+cur
    },0)
  }
  get filterAllNum(){
    return this.todos.length
  }
  get filterEatGram(){
    return this.todos.filter(todo=>todo.eat).map(todo=>todo.gram).reduce((acc,cur)=>{
      return acc+cur
    },0)
  }
  get filterEatNum(){
    return this.todos.filter(todo=>todo.eat).length
  }

}

export default TodoListStore